package com.hnichr.ump.salon.service.impl;

import com.hnichr.ump.common.salon.entity.Applicants;
import com.hnichr.ump.common.salon.entity.SalonInfo;
import com.hnichr.ump.common.salon.service.ApplicantsService;
import com.hnichr.ump.common.salon.service.SalonInfoService;
import com.hnichr.ump.common.service.SmsService;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ApplicantsServiceImpl extends BaseServiceImpl<Applicants> implements ApplicantsService {

    private static final String NOPASS_MSG = "%s，您的报名信息未审核通过，如有问题请联系%s，联系电话%s。";

    private static final String PASS_MSG = "%s，您的报名信息已审核通过，请于%s到%s参加%s活动，如有问题请联系%s，联系电话%s。";

    private static final String APP_KEY = "lJNj4ohL";
    private static final String APP_SECRET = "79caff0b74d39eccc8da27d24198c56155a46e83";

    @Autowired
    private SalonInfoService salonInfoService;

    @Autowired
    private SmsService smsService;


    @Transactional
    public void remove(String ids){
        String[] aId = ids.split(",");
        for(String id:aId){
            this.remove(Applicants.class,id);
        }
    }

    @Transactional
    public void apply(Applicants applicant){
        if(StringUtils.isBlank(applicant.getSalonId())){
            throw new MBootException("无会议信息");
        }

        List<Applicants> applicantsList = JpaUtil.linq(Applicants.class).equal("salonId",applicant.getSalonId()).equal("mobile",applicant.getMobile()).desc("createTime").list();
        if(CollectionUtils.isNotEmpty(applicantsList)){
            applicant.setId(applicantsList.get(0).getId());
            JpaUtil.update(applicant);
            return;
        }
        JpaUtil.save(applicant);
    }

    @Transactional
    public List<Applicants> listByMobile(String mobile){
        List<Applicants> applicantsList = JpaUtil.linq(Applicants.class).equal("mobile",mobile).desc("createTime").list();
        if(CollectionUtils.isEmpty(applicantsList)){
            return applicantsList;
        }
        for(Applicants applicants:applicantsList){
            SalonInfo salonInfo = salonInfoService.getById(applicants.getSalonId());
            applicants.setSalonInfo(salonInfo);
        }
        return applicantsList;
    }

    @Transactional
    public Applicants signIn(String salonId,String mobile){
        List<Applicants> applicantsList = JpaUtil.linq(Applicants.class).equal("salonId",salonId).equal("mobile",mobile).equal("status","1").desc("createTime").list();
        if(CollectionUtils.isEmpty(applicantsList)){
            throw new MBootException("抱歉，未查到您的报名信息，请联系工作人员");
        }
        for(Applicants applicants:applicantsList){
            applicants.setSignIn("1");
            applicants.setSignInTime(new Date());
        }
        JpaUtil.update(applicantsList);
        return applicantsList.get(0);
    }

    @Transactional
    public Applicants getApplicantInfo(String salonId,String mobile){
        List<Applicants> applicantsList = JpaUtil.linq(Applicants.class).equal("salonId",salonId).equal("mobile",mobile).desc("createTime").list();
        if(CollectionUtils.isNotEmpty(applicantsList)){
            return applicantsList.get(0);
        }
        //如果本次报名名单中没有，但是其他的会议中有，则带出最近的数据
        applicantsList = listByMobile(mobile);
        if(CollectionUtils.isNotEmpty(applicantsList)){
            return applicantsList.get(0);
        }
        return new Applicants();
    }

    @Transactional
    public void pass(String ids){
        List<Applicants> applicantList = new ArrayList<>();
        String[] idArray = ids.split(",");
        for(String id:idArray){
            Applicants applicants = this.getById(Applicants.class,id);
            applicants.setStatus("1");
            applicants.setAuditTime(new Date());
            JpaUtil.update(applicants);
            applicantList.add(applicants);
        }
        //发送短信
        for(Applicants a:applicantList){
            SalonInfo s = salonInfoService.getById(a.getSalonId());
            if(s==null){
                continue;
            }
            String msg = String.format(PASS_MSG,a.getName(), DateFormatUtils.format(s.getStartTime(),"yyyy年MM月dd日 HH点mm分"),s.getAddress(),s.getSalonName(),s.getContactsPr(),s.getContactsInfo());
            smsService.sendSms(msg,a.getMobile(),APP_KEY,APP_SECRET);
        }
    }

    @Transactional
    public void nopass(String ids){
        List<Applicants> applicantList = new ArrayList<>();
        String[] idArray = ids.split(",");
        for(String id:idArray){
            Applicants applicants = this.getById(Applicants.class,id);
            applicants.setStatus("2");
            applicants.setAuditTime(new Date());
            JpaUtil.update(applicants);
            applicantList.add(applicants);
        }
        //发送短信
        for(Applicants a:applicantList){
            SalonInfo s = salonInfoService.getById(a.getSalonId());
            if(s==null){
                continue;
            }
            String msg = String.format(NOPASS_MSG,a.getName(), s.getContactsPr(),s.getContactsInfo());
            smsService.sendSms(msg,a.getMobile(),APP_KEY,APP_SECRET);
        }
    }


}
